package com.hzf.material.admin.controller.manager;


import com.hzf.api.rpc.api.ApiService;
import com.hzf.material.dao.model.*;
import com.hzf.material.dao.model.MaterialGoodsTypeMiddleExample;
import com.hzf.material.rpc.api.MaterialGoodsTypeMiddleService;
import com.hzf.ucenter.rpc.common.constant.UcenterResult;
import com.hzf.ucenter.rpc.common.constant.UcenterResultConstant;
import com.hzf.upms.common.constant.UpmsResult;
import com.hzf.upms.common.constant.UpmsResultConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * describe: TODO
 * creat_user: 白开水
 * creat_time: 2017/11/20   18:44
 **/
@Controller
@Api(value = "材料商商品类型管理", description = "材料商商品管理")
@RequestMapping("/manage/goodsTypeMiddle")
public class MaterialGoodsTypeMiddleController {
    private static Logger _log = LoggerFactory.getLogger(MaterialGoodsTypeMiddleController.class);
    @Autowired
    private MaterialGoodsTypeMiddleService materialGoodsTypeMiddleService;

    @Autowired
    private ApiService apiService;


    @ApiOperation(value = "基础中类首页")
    @RequiresPermissions("	material:goodstypemiddle:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/materialGoodsTypeMiddle/index.jsp";
    }

    @ApiOperation(value = "基础中类列表")
    @RequiresPermissions("	material:goodstypemiddle:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        MaterialGoodsTypeMiddleExample materialGoodsTypeMiddleExample = new MaterialGoodsTypeMiddleExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            materialGoodsTypeMiddleExample.setOrderByClause(sort + " " + order);
        }
        List<MaterialGoodsTypeMiddle> rows = materialGoodsTypeMiddleService.selectByExampleForOffsetPage(materialGoodsTypeMiddleExample, offset, limit);
        long total = materialGoodsTypeMiddleService.countByExample(materialGoodsTypeMiddleExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增中类")
    @RequiresPermissions("	material:goodstypemiddle:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap) {
        MaterialGoodsTypeBigExample materialGoodsTypeBigExample = new MaterialGoodsTypeBigExample();
        List<MaterialGoodsTypeBig> list = apiService.selectByMaterialGoodsTypeBig(materialGoodsTypeBigExample);
        modelMap.put("typeBigList",list);
        return "/manage/materialGoodsTypeMiddle/create.jsp";
    }

    @ApiOperation(value = "新增中类")
    @RequiresPermissions("	material:goodstypemiddle:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(MaterialGoodsTypeMiddle materialGoodsTypeMiddle) {
        int count = materialGoodsTypeMiddleService.insertSelective(materialGoodsTypeMiddle);
        return new UcenterResult(UcenterResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除中类")
    @RequiresPermissions("	material:goodstypemiddle:delete")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = materialGoodsTypeMiddleService.deleteByPrimaryKeys(ids);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改中类")
    @RequiresPermissions("	material:goodstypemiddle:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        MaterialGoodsTypeMiddle materialGoodsTypeMiddle = materialGoodsTypeMiddleService.selectByPrimaryKey(id);
        MaterialGoodsTypeBigExample materialGoodsTypeBigExample = new MaterialGoodsTypeBigExample();
        List<MaterialGoodsTypeBig> list = apiService.selectByMaterialGoodsTypeBig(materialGoodsTypeBigExample);
        System.out.println("来到中类！！！！！");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getBigName());
        }
        modelMap.put("materialGoodsTypeMiddle", materialGoodsTypeMiddle);
        return "/manage/materialGoodsTypeMiddle/update.jsp";
    }

    @ApiOperation(value = "修改中类")
    @RequiresPermissions("	material:goodstypemiddle:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, MaterialGoodsTypeMiddle materialGoodsTypeMiddle) {
        materialGoodsTypeMiddle.setMiddleId(id);
        int count = materialGoodsTypeMiddleService.updateByPrimaryKeySelective(materialGoodsTypeMiddle);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

}
